﻿Imports FinanceiroBLL
Imports FinanceiroDAL
Public Class PagamentoFRM
    Dim objitBLL As New ItensCtasPagarBLL
    Dim objBLL As New ContasPagarBLL
    Public Codigo As Integer
    Public Codigoit As Integer
    Public dtemissao As Date
    Public Op As Char
    Dim stat As Integer
    Dim aux As Integer
    Dim vlrpago As Decimal = 0
    Dim desc As Decimal = 0
    Dim juros As Decimal = 0
    Dim vlrparcela As Decimal = 0
    Dim vlrfinal As Decimal = 0
    Dim valorpago As Decimal = 0
#Region "Load "
    Private Sub PagamentoFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        limpar()
        If (Op = "P") Then 'pagamento
            objitBLL = New ItensCtasPagarBLL(Codigoit, 1)
            carregaCombo()
            CarregaCampos()
            TXTValorPago.Text = TXTVrParcela.Text
        End If
    End Sub
#End Region
#Region "Carrega Campos"
    Sub CarregaCampos()
        LBCodigo.Text = "Código: " & objitBLL.ctpcodigo
        aux = objitBLL.icpcodigo
        TXTCod.Text = aux
        DTPDtpagamento.Text = Date.Now
        TXTVrParcela.Text = objitBLL.icp_valor
        CBFPagamentos.SelectedValue = objitBLL.pag_codigo
        DTPDtVencimento.Value = Date.Parse(objitBLL.icp_datavencimento)
        TXTValorPago.Text = objitBLL.icp_valorpago
        TXTDesconto.Text = objitBLL.icp_desconto
        TXTJuros.Text = objitBLL.icp_jam
        TXTNumparcela.Text = objitBLL.icp_parcela
        CBContas.SelectedValue = objitBLL.con_codigo
        TXTObs.Text = objitBLL.icp_boletocheque
        stat = objitBLL.sta_codigo
        DTPEmissao.Value = dtemissao
    End Sub
#End Region
#Region "carregaCombo"
    Sub carregaCombo()
        objconexao.carregaCombo(CBFPagamentos, "Generico.Gen_FormaPagamento", "pag_codigo", "pag_descricao", "pag_codigo=2 or pag_codigo=3 or pag_codigo >=6 and sta_codigo < 2", "pag_descricao")
        objconexao.carregaCombo(CBContas, "Financeiro.Ofi_contas", "con_codigo", "con_descricao", "sta_codigo=1", "con_descricao")
        CBFPagamentos.SelectedValue = -1
        CBContas.SelectedValue = -1
    End Sub
#End Region
#Region "Botão Pagar"
    Private Sub BPagar_Click(sender As System.Object, e As System.EventArgs) Handles BPagar.Click
        Decimal.TryParse(TXTValorPago.Text, vlrpago)
        Dim opt As Integer
        Dim data As Integer = 0
        vlrpago = TXTValorPago.Text
        Try
            data = DateTime.Compare(DTPEmissao.Value.ToShortDateString, DTPDtpagamento.Value.ToShortDateString)
            If Op = "P" Then
                If data <= 0 Then
                    If (vlrpago > 0) Then
                        If (CBContas.SelectedValue <> Nothing And CBFPagamentos.SelectedValue <> Nothing) Then
                            If (TXTValorPago.Text <> "" And TXTDesconto.Text <> "" And TXTJuros.Text <> "") Then
                                If (DTPDtVencimento.Value < DTPDtpagamento.Value) Then
                                    opt = MsgBox("Data de vencimento menor que a data de hoje, deseja realmente fechar?", MsgBoxStyle.OkCancel)
                                End If
                                If DTPDtVencimento.Value > DTPDtpagamento.Value Or opt = 1 Then
                                    opt = 1
                                    If opt = 1 Then
                                        objitBLL = New ItensCtasPagarBLL(Codigoit, Codigo, Date.Parse(DTPDtpagamento.Text), Decimal.Parse(TXTVrParcela.Text.Replace("R$", "")), CBFPagamentos.SelectedValue, Date.Parse(DTPDtVencimento.Text), Decimal.Parse(TXTValorPago.Text.Replace("R$", "")),
                                          Decimal.Parse(TXTDesconto.Text.Replace("R$", "")), Decimal.Parse(TXTJuros.Text.Replace("R$", "")), Val(TXTNumparcela.Text), CBContas.SelectedValue, "", 6)
                                        objitBLL.updateItensCtasPagar()
                                        MessageBox.Show("Pagamento efetuado com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                        limpar()
                                        Me.Close()
                                    End If
                                    objBLL.limpar()
                                    Me.Close()
                                End If
                            Else
                                MsgBox("Preencha os campos corretamente!", MsgBoxStyle.Exclamation) 'erro aki
                                If (TXTValorPago.Text = "") Then
                                    TXTValorPago.Select()
                                ElseIf TXTDesconto.Text = "" Then
                                    TXTDesconto.Select()
                                Else
                                    TXTJuros.Select()
                                End If
                            End If
                        Else
                            MsgBox("Selecione os campos contas e pagamentos corretamente!", MsgBoxStyle.Information)
                        End If
                    Else
                        MsgBox("Valor inválido. Verifique", MsgBoxStyle.Exclamation)
                        TXTValorPago.SelectAll()
                    End If
                Else
                    MsgBox("Data de pagamento não deve ser inferior a data de emissão", MsgBoxStyle.Critical)
                    DTPDtpagamento.Select()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region
#Region "Fechar"
    Private Sub BFechar_Click(sender As System.Object, e As System.EventArgs) Handles BFechar.Click
        limpar()
        objitBLL = Nothing
        objBLL = Nothing
        Me.Close()
    End Sub
#End Region
#Region "Limpar"
    Sub limpar()
        LBCodigo.Text = ""
        DTPDtpagamento.Text = ""
        TXTVrParcela.Text = "0,00"
        CBFPagamentos.SelectedValue = -1
        'DTPDtVencimento.Text = ""
        TXTValorPago.Text = "0,00"
        TXTDesconto.Text = "0,00"
        TXTJuros.Text = "0,00"
        TXTNumparcela.Text = ""
        CBContas.SelectedValue = -1
        TXTObs.Text = ""
        '  objitBLL.limpar()
    End Sub
#End Region
#Region "Atualiza valor"
    Sub atualizavalor()

        Dim valorfinal As Decimal = 0
        Dim ValorDesc, Valorparcela, juros As Decimal
        Try
            If (Decimal.TryParse(TXTDesconto.Text.Replace("R$", ""), ValorDesc)) Then
            Else : ValorDesc = "0,00"
            End If

            If (Decimal.TryParse(TXTVrParcela.Text.Replace("R$", ""), Valorparcela)) Then
            Else : Valorparcela = "0,00"
            End If

            If (Decimal.TryParse(TXTJuros.Text.Replace("R$", ""), juros)) Then
            Else : juros = 0
            End If

            If (Decimal.TryParse(TXTValorPago.Text.Replace("R$", ""), valorpago)) Then
            Else : valorpago = "0,00"
            End If

            If (Decimal.TryParse(TXTValorFinal.Text.Replace("R$", ""), valorfinal)) Then
            Else : valorfinal = "0,00"
            End If
            If desc = 0 Or juros = 0 Then
                TXTValorPago.Text = Valorparcela
            End If
            If (ValorDesc > Valorparcela) Then
                TXTValorPago.Text = Valorparcela - ValorDesc
                MsgBox("Desconto maior que valor total", MsgBoxStyle.Exclamation)
                TXTDesconto.Select()
            Else
                valorpago = (Valorparcela + juros) - ValorDesc
                TXTValorFinal.Text = valorpago
                TXTValorPago.Text = valorpago
            End If

            If TXTVrParcela.Text <> "" Then
                TXTVrParcela.Text = FormatCurrency(TXTVrParcela.Text)
            Else
                TXTVrParcela.Text = "0,00"
            End If
            If TXTDesconto.Text <> "" Then
                TXTDesconto.Text = FormatCurrency(TXTDesconto.Text)
            Else
                TXTDesconto.Text = "0,00"
            End If
            If TXTJuros.Text <> "" Then
                TXTJuros.Text = FormatCurrency(TXTJuros.Text)
            Else
                TXTJuros.Text = "0,00"
            End If
            If TXTValorPago.Text <> "" Then
                TXTValorPago.Text = FormatCurrency(TXTValorPago.Text)
            Else
                TXTValorPago.Text = "0,00"
            End If
            TXTValorFinal.Text = FormatCurrency(TXTValorFinal.Text)
            TXTValorPago.Text = FormatCurrency(TXTValorPago.Text)
        Catch ex As Exception
            Throw ex
        End Try

    End Sub
#End Region
#Region "Propriedades"
    Private Sub TXTValorPago_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTValorPago.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
        End If
    End Sub

    Private Sub TXTJuros_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTJuros.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
        If (TXTJuros.Text.Contains(",") = False And e.KeyChar = ",") Then
            e.Handled = False
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            atualizavalor()
        End If
    End Sub

    Private Sub TXTDesconto_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTDesconto.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
        If (TXTDesconto.Text.Contains(",") = False And e.KeyChar = ",") Then
            e.Handled = False
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            atualizavalor()
        End If
    End Sub

    Private Sub TXTVrParcela_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTVrParcela.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            atualizavalor()
            TXTVrParcela.Text = FormatCurrency(TXTVrParcela.Text.Replace("R$", "").Replace(" ", ""))
        End If
    End Sub
    Private Sub TXTDesconto_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles TXTDesconto.MouseUp
        TXTDesconto.SelectAll()
    End Sub

    Private Sub TXTDesconto_Leave(sender As Object, e As System.EventArgs) Handles TXTDesconto.Leave
        atualizavalor()
    End Sub

    Private Sub TXTVrParcela_Leave(sender As Object, e As System.EventArgs) Handles TXTVrParcela.Leave
        atualizavalor()
        TXTDesconto.SelectAll()
    End Sub

    Private Sub TXTVrParcela_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles TXTVrParcela.MouseUp
        TXTDesconto.SelectAll()
    End Sub

    Private Sub TXTJuros_Leave(sender As System.Object, e As System.EventArgs) Handles TXTJuros.Leave
        atualizavalor()
    End Sub
    Private Sub TXTValorFinal_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTValorFinal.TextChanged
        If (TXTValorFinal.SelectionStart) Then
            TXTValorPago.SelectAll()
        End If
    End Sub

    Private Sub TXTValorFinal_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles TXTValorFinal.MouseUp
        TXTValorFinal.SelectAll()
    End Sub

    Private Sub TXTValorFinal_Leave(sender As System.Object, e As System.EventArgs) Handles TXTValorFinal.Leave
        TXTValorFinal.Text = FormatCurrency(TXTValorFinal.Text.Replace("R$", "").Replace(" ", ""))
    End Sub

    Private Sub TXTValorFinal_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTValorFinal.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TXTValorFinal.Text = FormatCurrency(TXTValorFinal.Text.Replace("R$", "").Replace(" ", ""))
        End If
    End Sub

    Private Sub PagamentoFRM_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub TXTVrParcela_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTVrParcela.TextChanged
        TXTDesconto.SelectAll()
    End Sub

    Private Sub DTPDtpagamento_Leave(sender As System.Object, e As System.EventArgs) Handles DTPDtpagamento.Leave
        atualizavalor()
    End Sub
    Private Sub DTPDtpagamento_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles DTPDtpagamento.MouseUp
        If FormatDateTime(DTPDtVencimento.Text) < (Date.Now).ToShortDateString Then
            MsgBox("Data de vencimento menor que a data atual", MsgBoxStyle.Exclamation)
            DTPDtVencimento.Focus()
        End If
    End Sub
    Private Sub PagamentoFRM_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.Escape) Then 'sair com esc
            Me.Close()
        End If
    End Sub
#End Region

    Private Sub TXTValorPago_Leave(sender As System.Object, e As System.EventArgs) Handles TXTValorPago.Leave
        atualizavalor()
        TXTValorPago.Text = FormatCurrency(TXTValorPago.Text.Replace("R$", "").Replace(" ", ""))
    End Sub
End Class